

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class Solution
{
public:
    int add1(int a, int b)
    {
        int newxorsum = 0;
        int xorsum = a ^ b;
        int carry = (a & b) << 1;

        while (carry)
        {
            newxorsum = (xorsum & carry) << 1;
            xorsum ^= carry;
            carry = newxorsum;
        }

        return xorsum;
    }

    int add2(int a, int b)
    {
        while (b)
        {
            int carry = (a & b) << 1;
            a = a ^ b;
            b = carry;
        }

        return a;
    }

    vector<int> searchRange1(vector<int>& nums, int target)
    {
        int n = nums.size();
        if (!n)
            return { -1, -1 };

        int beg = 0, end = 0;
        int lf = 0, rg = n - 1, mid = 0;

        while (lf < rg)
        {
            mid = lf + ((rg - lf) >> 1);
            if (nums[mid] > target)
                rg = mid - 1;
            else if (nums[mid] < target)
                lf = mid + 1;
            else
                rg = mid;
        }
        if (nums[lf] != target)
            return { -1, -1 };
        beg = lf;

        rg = n - 1;
        while (lf < rg)
        {
            mid = lf + ((rg - lf + 1) >> 1);
            if (nums[mid] > target)
                rg = mid - 1;
            else if (nums[mid] < target)
                lf = mid + 1;
            else
                lf = mid;
        }
        end = rg;

        return { beg, end };
    }

    int lengthOfLastWord(string s)
    {
        int n = s.size();

        int beg = 0, end = n - 1;
        while (end >= 0 && isblank(s[end]))
        {
            --end;
        }

        beg = end;
        while (beg >= 0 && !isblank(s[beg]))
        {
            --beg;
        }

        return end - beg;
    }

    vector<int> searchRange1(vector<int>& nums, int target)
    {
        auto low = lower_bound(nums.begin(), nums.end(), target);
        if (low == nums.end() || *low != target)
            return { -1, -1 };

        auto upper = upper_bound(nums.begin(), nums.end(), target);

        return { static_cast<int>(low - nums.begin()), static_cast<int>(upper - nums.begin() - 1) };
    }

    vector<int> searchRange2(vector<int>& nums, int target)
    {
        int n = nums.size();
        if (!n)
            return { -1, -1 };

        int beg = 0, end = 0;
        int lf = 0, rg = n - 1, mid = 0;

        while (lf < rg)
        {
            mid = lf + ((rg - lf) >> 1);
            if (nums[mid] > target)
                rg = mid - 1;
            else if (nums[mid] < target)
                lf = mid + 1;
            else
                rg = mid;
        }
        if (nums[lf] != target)
            return { -1, -1 };
        beg = lf;

        rg = n - 1;
        while (lf < rg)
        {
            mid = lf + ((rg - lf + 1) >> 1);
            if (nums[mid] > target)
                rg = mid - 1;
            else if (nums[mid] < target)
                lf = mid + 1;
            else
                lf = mid;
        }
        end = rg;

        return { beg, end };
    }
};

void Test1()
{
    cout << Solution().add2(5, 3) << endl;
}

int main()
{
    Test1();

	return 0;
}